//
//  HYStatusViewModel.swift
//  iOSWeibo
//
//  Created by why on 16/7/25.
//  Copyright © 2016年 王浩宇. All rights reserved.
//

import UIKit

class HYStatusViewModel: NSObject {
    
    //单例
    static let sharedStatusViewModel: HYStatusViewModel = HYStatusViewModel()
    
    /// 加载微博数据的方法
    func loadStatus(completion:(error: NSError?, statusModels: [HYStatus]?) -> Void) {
        guard let access_token = HYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.access_token else {
            print("access_token 没有值")
            return
        }
        
        //urlString
        let urlString = "2/statuses/home_timeline.json"
        
        let parameters = [
            "access_token": access_token
        ]
        
        //发送请求
        HYNetworkTool.sharedInstance.GET(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) -> Void in
            
            //返回的字典
            let responseDict = responseObject as? [String: AnyObject]
            
            //获取 statuses key对应的value [String: AnyObject]
            let statusesArray =  responseDict?["statuses"] as? [[String: AnyObject]]
            
            //定义模型数组保存转好的微博模型
            var statusModels = [HYStatus]()
            
            //遍历数组里面的每个字典x
            for dict in statusesArray! {
                // 字典转模型
                let status = HYStatus(dict: dict)
                
                // 将模型添加到数组中
                statusModels.append(status)
            }
            
            // 模型就转好了
            completion(error:nil, statusModels:statusModels)
            
            }) { (_, error) -> Void in
                completion(error:error, statusModels:nil)
        }
    }
}
